package com.example.umeng_sdk;

import android.content.Context;

import com.umeng.analytics.MobclickAgent;
import com.umeng.analytics.AnalyticsConfig;
import com.umeng.commonsdk.UMConfigure;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

import java.util.List;
import java.util.Map;

public class MethodCallHandlerImpl implements MethodChannel.MethodCallHandler {
    private final MethodChannel channel;
    private final Context context;

    MethodCallHandlerImpl(MethodChannel channel, Context context) {
        this.channel = channel;
        this.context = context;
    }

    @Override
    public void onMethodCall(MethodCall call, MethodChannel.Result result) {
        try {
            switch (call.method) {
              case "getPlatformVersion":
                result.success("Android " + android.os.Build.VERSION.RELEASE);
              break;
                case "initCommon":
                    initCommon(call, result);
                    break;
                case "onEvent":
                    onEvent(call);
                    break;
                case "onProfileSignIn":
                    onProfileSignIn(call);
                    break;
                case "onProfileSignOff":
                    onProfileSignOff();
                    break;
                case "setPageCollectionModeAuto":
                    setPageCollectionModeAuto();
                    break;
                case "setPageCollectionModeManual":
                    setPageCollectionModeManual();
                    break;
                case "onPageStart":
                    onPageStart(call);
                    break;
                case "onPageEnd":
                    onPageEnd(call);
                    break;
                case "reportError":
                    reportError(call);
                    break;
                case "getChannelName":
                    getChannelName(result);
                    break;
                default:
                    result.notImplemented();
                    break;
            }
        } catch (Exception e) {
          e.printStackTrace();
          android.util.Log.e("Umeng", "Exception:"+e.getMessage());
        }
    }
    
    private void initCommon(MethodCall call, MethodChannel.Result result) {
        String channelName = AnalyticsConfig.getChannel(context);
        Boolean logEnabled = call.argument("logEnabled");
        if (logEnabled == null) {
            logEnabled = false;
        }
        UMConfigure.setLogEnabled(logEnabled);

        Boolean encryptEnabled = call.argument("encryptEnabled");
        if (encryptEnabled == null) {
            encryptEnabled = false;
        }
        UMConfigure.setEncryptEnabled(encryptEnabled);

        final String androidKey = call.argument("androidKey");
        final String channel;
        if(call.argument("channel") == null) {
            channel = channelName;
        }else {
            channel = call.argument("channel");
        }
        UMConfigure.init(context, androidKey, channel, UMConfigure.DEVICE_TYPE_PHONE, null);

        Integer sessionContinueMillis = call.argument("sessionContinueMillis");
        if (sessionContinueMillis == null) {
            sessionContinueMillis = 30000;
        }
        MobclickAgent.setSessionContinueMillis(sessionContinueMillis);

        Boolean catchUncaughtExceptions = call.argument("catchUncaughtExceptions");
        if (catchUncaughtExceptions == null) {
            catchUncaughtExceptions = true;
        }
        MobclickAgent.setCatchUncaughtExceptions(catchUncaughtExceptions);

        if ("MANUAL".equals(call.argument("pageCollectionMode"))) {
            MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.MANUAL);
        } else {
            MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);
        }
        android.util.Log.i("UMLog", "initCommon:"+androidKey+"@"+channel);
        result.success(channel);
    }

    private void onEvent(MethodCall call) {
        List args = (List) call.arguments;
        
        String event = (String)args.get(0);
        Map map = null ;
        if(args.size()>1) {
            map = (Map) args.get(1);
        }
        //JSONObject properties = new JSONObject(map);
        MobclickAgent.onEventObject(context, event, map);

        if(map!=null) {
            android.util.Log.i("UMLog", "onEventObject:"+event+"("+map.toString()+")");
        }
        else {
            android.util.Log.i("UMLog", "onEventObject:"+event);
        }
    }

    private void onProfileSignIn (MethodCall call) {
        List args = (List) call.arguments;
        String userID = (String)args.get(0);
        MobclickAgent.onProfileSignIn(userID);
        android.util.Log.i("UMLog", "onProfileSignIn:"+userID);
    }

    private void onProfileSignOff () {
        MobclickAgent.onProfileSignOff();
        android.util.Log.i("UMLog", "onProfileSignOff");
    }

    private void setPageCollectionModeAuto () {
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.LEGACY_AUTO);
        android.util.Log.i("UMLog", "setPageCollectionModeAuto");
    }

    private void setPageCollectionModeManual () {
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.LEGACY_MANUAL);
        android.util.Log.i("UMLog", "setPageCollectionModeManual");
    }

    private void onPageStart(MethodCall call) {
        List args = (List) call.arguments;
        String event = (String)args.get(0);
        MobclickAgent.onPageStart(event);
        android.util.Log.i("UMLog", "onPageStart:"+event);
    }

    private void onPageEnd(MethodCall call) {
        List args = (List) call.arguments;
        String event = (String)args.get(0);
        MobclickAgent.onPageEnd(event);
        android.util.Log.i("UMLog", "onPageEnd:"+event);
    }

    private void reportError(MethodCall call){
        List args = (List) call.arguments;
        String error = (String)args.get(0);
        MobclickAgent.reportError(context, error);
        android.util.Log.i("UMLog", "reportError:"+error);
    }
    private void getChannelName (MethodChannel.Result result) {
        String channelName = AnalyticsConfig.getChannel(context);
        result.success(channelName);
    }
}
